#ifndef __CAMERA_H__
#define __CAMERA_H__
#include "vec3.h"
#include "vec4.h"
#include "math_tool.h"

struct ClipPlane {
    // 平面上一点
    Vec4f point;
    // 平面法向量
    Vec4f normal; 
};

struct Frustum {
    // 近平面到原点z轴上距离
    float far;
    // 近平面到原点z轴上距离
    float near;
    // 水平视场角度
    float xFov;
    // 高宽比
    float aspect;  

    Frustum(float f, float n, float xf, float a)
        : far(f), near(n), xFov(xf), aspect(a) {
    }

    // 焦距
    const float e = 1 / tanf(radians(xFov / 2));
    // 垂直视场角度
    const float yFov = 2 * atanf(xFov / e);

    const float rateX = 1 / sqrtf(e * e + 1);
    const float rateY = 1 / sqrtf(e * e + aspect * aspect);

    const ClipPlane leftPlane  = {Vec4f{0, 0, 0, 1}, Vec4f{e * rateX, 0, -rateX, 0}};
    const ClipPlane rightPlane = {Vec4f{0, 0, 0, 1}, Vec4f{-e * rateX, 0, -rateX, 0}};
    const ClipPlane topPlane   = {Vec4f{0, 0, 0, 1}, Vec4f{0, -e * rateY, -aspect * rateY, 0}};
    const ClipPlane downPlane  = {Vec4f{0, 0, 0, 1}, Vec4f{0, e * rateY, -aspect * rateY, 0}};
    const ClipPlane nearPlane  = {Vec4f{0, 0, -near, 1}, Vec4f{0, 0, -1, -near}};
    const ClipPlane farPlane   = {Vec4f{0, 0, -far, 1}, Vec4f{0, 0, 1, -far}};
};

class Camera {
public:
    void move(const Vec3f& target) {
        position = position + target;
        center = center + target;
    }

    Vec3f position   = Vec3f{0, 0, 0};
    Vec3f center     = Vec3f{0, 0, -1};
    Vec3f up         = Vec3f{0, 1, 0};

    // float pitch;
    // float yaw;
};

#endif